package com.cskaoyan.service;

import com.cskaoyan.bean.Goods;
import com.cskaoyan.bean.GoodsExample;
import com.cskaoyan.bean.Role;
import com.cskaoyan.bean.RoleExample;
import com.cskaoyan.bo.CreateRoleBo;
import com.cskaoyan.mapper.RoleMapper;
import com.cskaoyan.vo.GoodsListVo;
import com.cskaoyan.vo.RoleListVo;
import com.cskaoyan.vo.RoleOptionsVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    /**
     * 功能:管理员页面role/options
     * 作者:whj
     */
    @Override
    public List<RoleOptionsVo> query() {
        List<RoleOptionsVo> optionsVos = new ArrayList<>();
        List<Role> roles = roleMapper.selectByExample(new RoleExample());
        for (Role role : roles) {
            RoleOptionsVo optionsVo = new RoleOptionsVo();
            optionsVo.setValue(role.getId());
            optionsVo.setLabel(role.getName());
            optionsVos.add(optionsVo);
        }
        return optionsVos;
    }

    @Override
    public RoleListVo list(Integer page, Integer limit, String name, String sort, String order) {

        PageHelper.startPage(page, limit);

        RoleExample roleExample = new RoleExample();
        roleExample.setOrderByClause(sort + " " + order);
        RoleExample.Criteria criteria = roleExample.createCriteria();
        //模糊查询条件
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        //只查询未删除的角色
        criteria.andDeletedEqualTo(false);
        List<Role> roleList = roleMapper.selectByExample(roleExample);

        //通过PageInfo获取分页信息
        PageInfo<Role> rolePageInfo = new PageInfo<>(roleList);
        long total = rolePageInfo.getTotal();

        RoleListVo roleListVo = new RoleListVo();
        roleListVo.setTotal(total);
        roleListVo.setItems(roleList);
        return roleListVo;
    }

    @Override
    public Role create(CreateRoleBo createRoleBo) {
        Role role = new Role();

        role.setName(createRoleBo.getName());
        role.setDesc(createRoleBo.getDesc());
        role.setEnabled(true);
        role.setAddTime(new Date(System.currentTimeMillis()));
        role.setUpdateTime(new Date(System.currentTimeMillis()));
        role.setDeleted(false);

        int insert = roleMapper.insert(role);
        if (insert <= 0) {
            return null;
        }
        return role;
    }

    @Override
    public int delete(Role role) {

        role.setDeleted(true);
        int i = roleMapper.updateByPrimaryKey(role);

        if (i <= 0) {
            return 404;
        }
        return 200;
    }

    @Override
    public int update(Role role) {

        int i = roleMapper.updateByPrimaryKey(role);
        if (i <= 0) {
            return 404;
        }
        return 200;
    }
}
